﻿using System;
using RBOSS.Business;
using RBOSS.Business.CustomerModule;
using RBOSS.Utilities;

namespace RBOSS.WebUI
{
    public partial class Register : System.Web.UI.Page
    {
        private CustomerSubSystem _customerSubSystem;

        protected void Page_Load(object sender, EventArgs e)
        {
            _customerSubSystem = new CustomerSubSystem();
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if(!_customerSubSystem.CheckEmailAvailability(txtUserEmail.Text))
            {
                FailureText.Text = "Email address is already registered...";
                ErrorMessage.Visible = true;
                return;
            }

            var userEmail = txtUserEmail.Text;
            var password = RBOSSecurity.Encrypt(txtPassword.Text);
            var firstName = txtFirstName.Text;
            var lastName = txtLastName.Text;

            var phoneNo = txtPhone.Text;
            var city = txtCity.Text;
            var provience = txtProvience.Text;
            var country = txtCountry.Text;
            var postalCode = txtPostalCode.Text;

            _customerSubSystem.AddCustomer(userEmail, password, firstName, lastName, phoneNo, city, provience, country, postalCode);

            FailureText.Text = "New User Registered Successfully!!! Please Login!!!";
            ErrorMessage.Visible = true;
            ClearFields();
        }

        private void ClearFields()
        {
            txtUserEmail.Text = string.Empty;
            txtPassword.Text = string.Empty;

            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;

            txtPhone.Text = string.Empty;
            txtCity.Text = string.Empty;

            txtProvience.Text = string.Empty;
            txtCountry.Text = string.Empty;

            txtPostalCode.Text = string.Empty;
        }
    }
}